#include <iostream>
#include <string>
#include <random>
#include <sstream>
#include <atomic>
#include <iomanip>
#include<thread>
#include "Detail.hpp"
#include "Message.hpp"
#include"Net.hpp"

using namespace std;


//rpcrequest测试
void test1()
{
    zgwrpc::RpcRequest::ptr rrp = zgwrpc::MessageFactory::create<zgwrpc::RpcRequest>();
    Json::Value param;
    param["num1"] = 11;
    param["num2"] = 22;
    rrp->setMethod("Add");
    rrp->setParams(param);
    rrp->setOpType(zgwrpc::Rtype::REQ_ASYNC);

    std::string str = rrp->serialize();

    std::cout << "序列化后的字符串" << str << std::endl;
    zgwrpc::BaseMessage::ptr bmp = zgwrpc::MessageFactory::createMessage(zgwrpc::MType::REQ_RPC);
    int n = bmp->unSerialize(str);
    zgwrpc::RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<zgwrpc::RpcRequest>(bmp);
    if (n > 0)
    {
        if(rrp2->check())
        {
            std::cout << "返序列化后的字符串" << rrp2->method() << std::endl;
            cout<<"params:";
            Json::Value v1=rrp2->params();
            int n=v1.size();
            cout<<"size:"<<n<<endl;
            if(rrp2->method()=="Add")
            {
                cout<<"num1:"<<v1["num1"].asInt()<<endl;
                cout<<"num2:"<<v1["num2"].asInt()<<endl;
            }
        }        
        cout<<endl;
    }
    else
    {
        cout << "反序列化失败" << endl;
    }
}

//topic测试
void test2()
{
    zgwrpc::TopicRequest::ptr trp=zgwrpc::MessageFactory::create<zgwrpc::TopicRequest>();
    zgwrpc::BaseMessage::ptr bmp=zgwrpc::MessageFactory::createMessage(zgwrpc::MType::REQ_TOPIC);
    trp->setTopicOpType(zgwrpc::TopicOpType::TOPIC_SUBSCRIBE);
    trp->setTopicKey("原神");
    trp->setTopicMsg("欢迎来到原神,请选择你的角色----1.小草神   2.雷电将军");

    string str=trp->serialize();
    cout<<"序列化后的字符串:"<<str<<endl;

    zgwrpc::TopicRequest::ptr trp2=std::dynamic_pointer_cast<zgwrpc::TopicRequest>(bmp);
    trp2->unSerialize(str);

    if(trp2->check())
    {
        cout<<"topic: "<<endl;
        cout<<trp2->topicKey()<<endl;
        cout<<"TopicOpType: "<<(int)trp2->topicOpType()<<endl;
        cout<<"Message:"<<trp2->topicMsg();
        cout<<endl;
    }
    else 
    {
        std::cout << "数据存在错误" << endl;
    }
}

//service测试
void test3()
{
    zgwrpc::ServiceRequest::ptr srp=zgwrpc::MessageFactory::create<zgwrpc::ServiceRequest>();
    zgwrpc::BaseMessage::ptr bmp=zgwrpc::MessageFactory::createMessage(zgwrpc::MType::REQ_SERVICE);
    srp->setMethod("Add");
    srp->setServiceOpType(zgwrpc::ServiceOpType::SERVICE_DISCOVERY);
    srp->setHost(make_pair("127.0.0.1",8001));
    string str=srp->serialize();
    cout<<"序列化后的字符串:"<<str<<endl;

    zgwrpc::ServiceRequest::ptr srp2=std::dynamic_pointer_cast<zgwrpc::ServiceRequest>(bmp);
    srp2->unSerialize(str);
    if(srp2->check())
    {
        cout<<"method: "<<srp2->method()<<endl;
        cout<<"ServiceOpType: "<<(int)srp2->serviceOpType()<<endl;
        cout<<"Host: "<<srp2->host().first<<":"<<srp2->host().second<<endl;
    }
    else 
    {
        cout<<"参数出错"<<endl;
    }
}

//rpcrespond侧式
void test4()
{
    zgwrpc::RpcResponse::ptr rrp=zgwrpc::MessageFactory::create<zgwrpc::RpcResponse>();
    zgwrpc::BaseMessage::ptr bmp=zgwrpc::MessageFactory::createMessage(zgwrpc::MType::RSP_RPC);

    rrp->setRCode(zgwrpc::RCode::RCODE_OK);
    Json::Value v1;
    v1["result"] = 33;
    rrp->setResult(v1);
    string str=rrp->serialize();

    zgwrpc::RpcResponse::ptr rrp2=std::dynamic_pointer_cast<zgwrpc::RpcResponse>(bmp);
    rrp2->unSerialize(str);
    if(rrp2->check())
    {
        cout<<"RCode: "<<(int)rrp2->rCode()<<endl;
        cout<<"Result: "<<rrp2->result()["result"].asInt()<<endl;
    }
    else 
    {
        cout<<"数据出错"<<endl;
    }
}

//responsetopic侧式
void test5()
{
    zgwrpc::TopicResponse::ptr trp=zgwrpc::MessageFactory::create<zgwrpc::TopicResponse>();
    zgwrpc::BaseMessage::ptr bmp=zgwrpc::MessageFactory::createMessage(zgwrpc::MType::RSP_TOPIC);
    trp->setRCode(zgwrpc::RCode::RCODE_OK);
}

//responseservice侧式
void test6()
{
    zgwrpc::ServiceResponse::ptr srp=zgwrpc::MessageFactory::create<zgwrpc::ServiceResponse>();
    zgwrpc::BaseMessage::ptr bmp=zgwrpc::MessageFactory::createMessage(zgwrpc::MType::RSP_SERVICE);
    srp->setRCode(zgwrpc::RCode::RCODE_OK);
    srp->setMethod("Add");
    vector<zgwrpc::Address> hosts;
    hosts.push_back(make_pair("127.0.0.1",8001));
    hosts.push_back(make_pair("127.0.0.1",8002));
    srp->setHost(hosts);
    string str=srp->serialize();

    zgwrpc::ServiceResponse::ptr srp2=std::dynamic_pointer_cast<zgwrpc::ServiceResponse>(bmp);
    srp2->unSerialize(str);
    if(srp2->check())
    {
        cout<<"RCode: "<<(int)srp2->rCode()<<endl;
        cout<<"Method: "<<srp2->method()<<endl;
        cout<<"Host: "<<srp2->hosts()[0].first<<":"<<srp2->hosts()[0].second<<endl;
        cout<<"Host: "<<srp2->hosts()[1].first<<":"<<srp2->hosts()[1].second<<endl;
    }
    else 
    {
        cout<<"数据出错"<<endl;
    }
    
}
#include"Dispatcher.hpp"


void OnMessage_Rpc(const zgwrpc::BaseConnection::ptr& conn, const zgwrpc::RpcResponse::ptr& msg)
{
    std::string str=msg->serialize();
    std::cout<<"收到消息:"<<str<<std::endl;
}

void OnMessage_Topic(const zgwrpc::BaseConnection::ptr& conn, const zgwrpc::TopicResponse::ptr& msg)
{
    LOG(LogLevel::INFO)<<"收到主题消息";
    string msg_str=msg->serialize();
    cout<<"收到主题消息:"<<msg_str<<endl;
}



void test7()
{
    zgwrpc::RpcRequest::ptr rrp = zgwrpc::MessageFactory::create<zgwrpc::RpcRequest>();
    Json::Value param;
    param["num1"] = 11;
    param["num2"] = 22;
    rrp->setId("222");
    rrp->setMethod("Add");
    rrp->setParams(param);
    rrp->setOpType(zgwrpc::Rtype::REQ_ASYNC);
    rrp->setMType(zgwrpc::MType::REQ_RPC);

    zgwrpc::TopicRequest::ptr trp=zgwrpc::MessageFactory::create<zgwrpc::TopicRequest>();
    trp->setTopicKey("原神");
    trp->setId("666");
    trp->setTopicOpType(zgwrpc::TopicOpType::TOPIC_SUBSCRIBE);
    trp->setTopicMsg("欢迎来到原神,请选择你的角色----1.小草神   2.雷电将军");
    trp->setMType(zgwrpc::MType::REQ_TOPIC);

    zgwrpc::Dispatcher::ptr dispatcher=zgwrpc::DispatcherFactory::create();
    dispatcher->registerHandler<zgwrpc::RpcResponse>(zgwrpc::MType::RSP_RPC,OnMessage_Rpc);
    dispatcher->registerHandler<zgwrpc::TopicResponse>(zgwrpc::MType::RSP_TOPIC,OnMessage_Topic);
    
    
    // using ConnectionCallback = std::function<void(const BaseConnection::ptr &conn)>;
    // using MessageCallback = std::function<void(const BaseConnection::ptr &conn, const BaseMessage::ptr &buf)>;
    // using CloseCallback = std::function<void(const BaseConnection::ptr &conn)>;
    
    //-----------
    auto client=zgwrpc::ClientFactory::create(9090,"127.0.0.1");
    client->setMessageCallback(bind(&zgwrpc::Dispatcher::dispatch,dispatcher,placeholders::_1,placeholders::_2));
    
    cout<<"连接成功"<<endl;
    client->send(rrp);
    client->send(trp);

    std::this_thread::sleep_for(std::chrono::seconds(1));
    client->shutdown();
}

int main()
{
    test7();
    return 0;
}